<template>
  <PageListContainer class="notice-list">
    <div class="base-block">
      <k-search :model="state.params" @search="handleSearch()" @reset="methods.onReset">
        <el-form-item label="搜索">
          <el-input v-model="state.params.keyword" style="width: 222px" clearable placeholder="请输入商品信息、商品ID、店铺" />
        </el-form-item>
        <el-form-item label="团长">
          <el-input v-model="state.params.colonel_key_word" clearable placeholder="请输入团长ID，名称" />
        </el-form-item>
        <el-form-item label="商品状态">
          <el-select v-model="state.params.status">
            <el-option v-for="(o, i) in statusOptions" :key="i" :value="o.value" :label="o.label" />
          </el-select>
        </el-form-item>
        <el-form-item label="关联类型">
          <el-select v-model="state.params.colonel_type">
            <el-option v-for="(o, i) in relevanceOptions" :key="i" :value="o.value" :label="o.label" />
          </el-select>
        </el-form-item>
      </k-search>
    </div>
    <div class="base-block">
      <k-toolbar>
        <div class="toolbar">
          <el-button
            v-if="[1, ''].includes(currentParams.status)"
            type="primary"
            :disabled="currentParams.status === 3 || state.selectedList.length < 1"
            @click="onMultipleChange(2)"
          >批量下架</el-button>
          <el-button
            v-if="[2, ''].includes(currentParams.status)"
            type="primary"
            :disabled="currentParams.status === 3 || state.selectedList.length < 1"
            @click="onMultipleChange(1)"
          >批量上架</el-button>
          <div class="select">
            当前已选<span class="len">{{ state.selectedList.length }}</span>个商品
          </div>
        </div>
      </k-toolbar>
      <k-table v-bind="tableState" v-on="tableEvent" @selectionChange="selectionChange">
        <!-- 商品信息 -->
        <template #cover="{ row }">
          <div class="info">
            <el-image :src="row.cover" class="img" alt="cover" @click="onClickDetail(row)" />
            <div class="right">
              <div class="title-box">
                <EllipsisText :content="row.title" :width="145" show-tooltip @click="onClickDetail(row)" />
                <SvgIcon class="icon" name="juejin-link" @click="onClickDetail(row)" />

                <el-tooltip :content="row.product_id" placement="top">
                  <el-icon class="copy-btn ml-4" :size="11" @click="handleCopyText(row.product_id)"><CopyDocument /></el-icon>
                </el-tooltip>
              </div>

              <p class="name base-text-ellipsis">{{ row.colonel_name }}</p>
            </div>
          </div>
        </template>
        <!-- 活动id -->
        <template #activity="{ row }">
          <BaseCopy :value="row.activity_id+''" :width="145" show-copy show-tooltip />
        </template>
        <!-- 店铺 -->
        <template #shop="{ row }">
          <div class="shop">
            <EllipsisText :content="row.shop_name" :width="96" :custom-style="{ textAlign: 'center', minWidth: '100%' }" show-tooltip />
            <!-- <p class="title base-text-ellipsis">{{ row.shop_name }}</p> -->
            <!-- <p class="level" :class="getColor(row.reading,4.71,4.40).color">
              {{ row.reading }}
              {{ getColor(row.reading,4.71,4.40).level }}
            </p> -->
            <ScoreLevel :number="row.shop_score" />
          </div>
        </template>
        <!-- 佣金 -->
        <template #payment="{ row }">
          <div class="payment">
            <div class="title base-text-ellipsis">{{ ((row.colonel_cos_ratio * 100000) / 1000).toFixed(1) + '%' }}</div>
            <div v-if="row.is_high_cos_ratio" class="tag">高佣</div>
          </div>
        </template>
        <!-- 商品状态 -->
        <template #status="{ row }">
          <div class="status">
            {{ row.status === 1 ? '上架中' : row.status === 2 ? '已下架' : row.status === 3 ? '已失效' : '' }}
          </div>
        </template>
        <!-- 关联橱窗数 -->
        <template #relative="{ row }">
          <div class="relative" @click="onClickRelative(row)">
            {{ row.shop_count }}
          </div>
        </template>
        <template #put_time="{ row }">
          <div class="flex-center">
            {{ row.put_time === 0 ? '--' : formatDateTime(row.put_time) }}
          </div>
        </template>
        <!-- 操作 -->
        <template #actions="{ row }">
          <el-link type="primary" @click="onClickDetail(row)">查看</el-link>
          <!-- 下架状态的时候禁用 -->
          <el-link v-if="row.status === 2" type="danger" @click="onClickChangeStatus(row, 1)">上架</el-link>
          <el-link v-else-if="row.status === 1" type="danger" @click="onClickChangeStatus(row, 2)">下架</el-link>
        </template>
      </k-table>
    </div>
    <!-- 关联商品 -->
    <RelativeListModal ref="relativeListModal" />
    <!-- 详情 -->
    <DetailModal ref="detailModal" />
  </PageListContainer>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import { formatDateTime, toolCopyText } from '@/utils';
import { getSkuDown, getMultiSkuDown, reqSkuPut, reqMultiSkuPut } from '@/api/crm-kgj/sku';
import { statusOptions, relevanceOptions, useTableList } from './hooks/use-table';
import { baseConfirm } from '@crud/index';
import SvgIcon from '@/components/svg-icon/svg-icon.vue';
import RelativeListModal from './components/relative-list-modal.vue';
import DetailModal from './components/detail-modal.vue';
import ScoreLevel from './components/score-level.vue';
import BaseCopy from '@/components/base-copy/index.vue';
import EllipsisText from '@/components/base-ellipsis-text/index.vue';
import { CopyDocument } from '@element-plus/icons-vue';
defineOptions({ name: 'ContentArticleList' });
const { state, tableState, tableEvent, ...methods } = useTableList();

const currentParams = ref({ status: '' });
const onClickChangeStatus = async (row: any, targetStatus: number) => {
  const handle = targetStatus === 1 ? '上架' : '下架';
  try {
    if (targetStatus === 1) {
      /** 如果要上架 */
      await baseConfirm({ type: 'none', title: `${handle}商品`, message: `是否${handle}所选商品？如需继续请点击确认。` });
      await reqSkuPut(row.id);
      // ElMessage.success('操作成功');
      // methods.getList();
    } else {
      /** 如果要下架 */
      await baseConfirm({ type: 'none', title: `${handle}商品`, message: `该商品状态将变更${handle}状态，无法在掘金展示，如需继续请点击确认。` });
      // await changeStatus({ id: row.id, is_ban: row.is_ban ? 0 : 1 });
      await getSkuDown(row.id);
    }
    ElMessage.success('操作成功');
    methods.getList();
  } catch (e) {
    console.log(e);
  }
};
const onMultipleChange = async (targetStatus: number) => {
  const handle = targetStatus === 1 ? '上架' : '下架';
  if (targetStatus === 1) {
    /** 如果要上架 */
    const ids = state.selectedList.filter((e) => e.status === 2).map((item: any) => item.id);
    await baseConfirm({ type: 'none', title: `${handle}商品`, message: `是否${handle}所选商品？如需继续请点击确认。` });
    await reqMultiSkuPut(ids);
  } else {
    /** 如果要下架 */
    const ids = state.selectedList.filter((e) => e.status === 1).map((item: any) => item.id);
    await baseConfirm({ type: 'none', title: `${handle}商品`, message: `该商品状态将变更${handle}状态，无法在掘金展示，如需继续请点击确认。` });
    await getMultiSkuDown(ids);
  }
  ElMessage.success('操作成功');
  methods.getList();
};
const selectionChange = (arr: any) => {
  state.selectedList = arr;
};
const handleCopyText = (text:any) => {
  toolCopyText(text);
  ElMessage.success('复制成功');
};
// 查看
const detailModal = ref();
const onClickDetail = (row: any) => {
  detailModal.value.show(0, row);
};
// 关联橱窗
const relativeListModal = ref();
const onClickRelative = (row: any) => {
  relativeListModal.value.show(0, row);
};

const handleSearch = () => {
  methods.onSearch();
  /** 将搜索条件缓存，用做判断 */
  currentParams.value = JSON.parse(JSON.stringify(state.params));
};
</script>
<style lang="scss">
.cover-wrapper {
  width: 160px;
}

.toolbar {
  display: flex;
  align-items: center;

  .select {
    margin-left: 16px;
    font-size: 14px;
    line-height: 22px;
    color: #909399;

    .len {
      padding: 0 2px;
      color: #606266;
    }
  }
}

.shop {
  .title {
    height: 23px;
    font-size: 14px;
    font-weight: 400;
    line-height: 23px;
    color: #303133;
    letter-spacing: 0;
  }

  .level {
    font-size: 12px;
    font-weight: 400;
    line-height: 20px;
    color: #a8abb2;

    &.danger {
      color: #f56c6c;
    }

    &.warning {
      color: #e6a23c;
    }
  }
}

.info {
  display: flex;
  align-items: center;
  cursor: pointer;

  .img {
    flex-shrink: 0;
    width: 47px;
    height: 47px;
  }

  .right {
    flex: 1;
    margin-left: 8px;
    overflow: hidden;

    .title-box {
      display: flex;
      align-items: center;
      color: #303133;
    }

    .icon {
      flex-shrink: 0;
      width: 14px;
      height: 14px;
      margin-left: 2px;
      &:hover{
        color: #4975e9;
      }
    }
    .copy-btn{
      &:hover{
        color: #4975e9;
      }
    }

    .title {
      min-width: 0;
      height: 23px;
      overflow: hidden;
      font-size: 14px;
      font-weight: 400;
      line-height: 23px;
      color: #303133;
      &:hover{
        color: #4975e9;
      }
    }

    .name {
      font-size: 12px;
      font-weight: 400;
      line-height: 20px;
      color: #909399;
    }
  }

}

.payment {
  display: flex;
  flex-direction: row-reverse;
  align-items: center;

  .tag {
    width: 38px;
    height: 18px;
    padding: 0 4px;
    margin-right: 4px;
    font-size: 12px;
    font-weight: 400;
    line-height: 18px;
    color: #ec803d;
    text-align: center;
    background-color: #fff1ec;
    border: 1px #ec803d solid;
    border-radius: 2px;
  }
}

.relative {
  color: #4975e9;
  cursor: pointer;
}
</style>
